package com.estudy.service.impl;

import com.estudy.domain.User;
import com.estudy.exception.LoginException;
import com.estudy.mapper.UserMapper;
import com.estudy.service.UserService;
import com.estudy.utils.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public String login(User user) {
        String username = user.getUsername();
        User userDetails = userMapper.findByUsername(username);
        if (userDetails == null) {
            throw new LoginException("用户不存在");
        }
        if (!user.getPassword().equals(userDetails.getPassword())) {
            throw new LoginException("密码错误");
        }
        return JwtTokenUtil.generateToken(user);
    }
}
